#!/usr/bin/env python3

# 1.实现一个求和函数
def calc_sum(*args):
    ax = 0
    for n in args:
        ax = ax + n
    return ax


# 2.实现一个懒加载的求和函数
def lazy_sum(*args):
    def sum1():
        ax = 0
        for n in args:
            ax = ax + n
        return ax

    return sum1


f = lazy_sum(1, 2, 3, 4, 5)
f2 = lazy_sum(1, 2, 3, 4, 5)

print(f())
print(f == f2)


# nonlocal 闭包
def create_counter():
    x = 0

    def counter():
        nonlocal x
        x = x + 1
        return x

    return counter


# 测试:
counterA = create_counter()
print(counterA(), counterA(), counterA(), counterA(), counterA())  # 1 2 3 4 5
counterB = create_counter()
if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
    print('测试通过!')
else:
    print('测试失败!')
